package com.friend.base.controller.base;


import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.friend.base.service.base.AssistRecordService;
import com.friend.base.service.base.FriendNoteService;
import com.friend.base.service.base.MemberCircleService;
import com.friend.base.service.base.MemberService;
import com.friend.common.model.BaseController;
import com.friend.common.model.Result;
import com.friend.entity.entity.base.AssistRecord;
import com.friend.entity.entity.base.FriendNote;
import com.friend.entity.entity.base.Member;
import com.friend.entity.entity.base.MemberCircle;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author friend
 * @since 2019-12-31
 */
@Api(tags = {"道友记前端控制器"})
@Log4j2
@RestController
@RequestMapping(value = "/friendNote", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
public class FriendNoteController extends BaseController {

    @Autowired
    private MemberService memberService;
    @Autowired
    private FriendNoteService friendNoteService;
    @Autowired
    private MemberCircleService memberCircleService;
    @Autowired
    private AssistRecordService assistRecordService;

    @GetMapping("/getNoteList")
    @ApiOperation(value = "查询道友记", notes = "根据会员查询关联的道友记", httpMethod = "GET")
    public Result getNoteList(@RequestParam("memId") Integer memId){
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
        try {
            List<FriendNote> notes = new ArrayList<>();
            List<MemberCircle> memCircs = memberCircleService.list(Wrappers.<MemberCircle>lambdaQuery().eq(MemberCircle::getMemId, memId));
            List<Integer> cirIds = memCircs.stream().map(memCir -> memCir.getCircleId()).collect(Collectors.toList());
            if (CollectionUtil.isNotEmpty(cirIds)){
                List<MemberCircle> memCirs = memberCircleService.list(Wrappers.<MemberCircle>lambdaQuery().in(MemberCircle::getCircleId, cirIds));
                List<Integer> memIds = memCirs.stream().map(memCir -> memCir.getMemId()).collect(Collectors.toList());
                if (CollectionUtil.isNotEmpty(memIds)){
                    notes = friendNoteService.list(Wrappers.<FriendNote>lambdaQuery().in(FriendNote::getPublUser, memIds));
                }
            }
            notes.forEach(note -> {
                Member member = memberService.getById(note.getPublUser());
                List<AssistRecord> list = assistRecordService.list(Wrappers.<AssistRecord>lambdaQuery().eq(AssistRecord::getNoteId, note.getId()).eq(AssistRecord::getMemId, memId).eq(AssistRecord::getDelFlag, 0));
                if (CollectionUtil.isNotEmpty(list)) note.setIfAssist(1);
                List<AssistRecord> recordList = assistRecordService.list(Wrappers.<AssistRecord>lambdaQuery().eq(AssistRecord::getNoteId, note.getId()).eq(AssistRecord::getDelFlag, 0));
                note.setNickname(member.getNickname()).setFaceImage(member.getFaceImage()).setSex(member.getSex())
                .setPubTimeStr(note.getPubTime().format(formatter)).setAssist(recordList.size());
            });
            return this.createSuccessResult(notes);
        }catch (Exception e){
            e.printStackTrace();
            return this.createFailedResult("道友记查询失败");
        }
    }


    @PostMapping("/saveNote")
    @ApiOperation(value = "保存道友记", notes = "保存会员发布的道友记", httpMethod = "POST")
    public Result saveNote(@RequestBody FriendNote friendNote){
        try {
            boolean flag = friendNoteService.save(friendNote);
            if (flag){
                log.info("保存道友记,保存时间date-{},道友记信息-{}",LocalDate.now(),friendNote);
                return this.createSuccessResult(flag);
            }
            return this.createFailedResult("保存道友记失败");
        }catch (Exception e){
            e.printStackTrace();
            return this.createFailedResult("保存道友记失败");
        }
    }
}

